#!/usr/bin/env python3
# Author: Armit
# Create Time: 2022/11/18 

# 绘制每个特征维度数值分布的直方图，辅助判断如何做数值正则化

from pathlib import Path

import numpy as np
import matplotlib.pyplot as plt

from data import get_df, LOG_PATH, FEATURE_NUM


def plot_stats():
  print('[plot_stats]')

  out_dp = Path(LOG_PATH) / 'arip_stats'
  out_dp.mkdir(exist_ok=True)

  df = get_df()
  for _, col in enumerate(FEATURE_NUM):
    v = df[col].astype(np.float32)
    v_log = np.log(v + 1e-5)
    EX, DX = v_log.mean(), v_log.std()
    v_clip = v_log.clip(EX - 3 * DX, EX + 3 * DX)

    plt.clf()
    plt.subplot(311) ; plt.title('raw')      ; plt.hist(v,      bins=100)
    plt.subplot(312) ; plt.title('log')      ; plt.hist(v_log,  bins=100)
    plt.subplot(313) ; plt.title('log-clip') ; plt.hist(v_clip, bins=100)
    plt.suptitle(col)
    plt.tight_layout()

    fp = out_dp / f'{col}.png'
    print(f'=> {fp}')
    plt.savefig(fp, dpi=400)


if __name__ == '__main__':
  plot_stats()
